package cn.qn.repository.security;

import cn.qn.moudle.security.Role;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * 角色持久层
 * @author Jeet
 * 2018年6月5日 12:53:55
 */
@Repository
public interface RoleRepository extends JpaRepository<Role,Integer> {

    /**
     * 通过角色CODE查询
     * @param code
     * @return
     */
    Role findByCode(String code);

    /**
     * 分页查询
     * @return
     */
    @Query(value = "SELECT * FROM security_role",
    countQuery = "SELECT count(*) FROM security_role",nativeQuery = true)
    Page<Role> findPage(Pageable pageable);

    @Query(value = "select r.* from security_role r LEFT JOIN security_member_role mr ON r.id=mr.role_id where mr.user_id=?1",nativeQuery=true)
    List<Role> findByUserId(String userId);

    @Query(value = "select r.* from security_role r LEFT JOIN security_role_menu rm ON r.id=rm.role_id where rm.menu_id=?1",nativeQuery=true)
    List<Role> findByMenuId(Integer menuId);

    @Query(value = "select r.* from security_role r LEFT JOIN security_role_permission rp ON r.id=rp.role_id where rp.permission_id=?1 AND r.status=1",nativeQuery=true)
    List<Role> findRolesByPermissionId(Integer permissionId);

    @Query(value = "SELECT r.* FROM security_role r where  r.type=?1",nativeQuery = true)
    List<Role> findRolesByType(Integer type);

}
